//
//  RegisterBindingVC.m
//  StandardizedProduct
//
//  Created by 洛水寒 on 16/7/25.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "RegisterBindingVC.h"
#import "JHSelector.h"

@interface RegisterBindingVC ()<UITextFieldDelegate>
{
    NSString *_nameString;
     NSString *_pswString;
}
@end

@implementation RegisterBindingVC
-(id)initWithName:(NSString *)nameString withPsw:(NSString *)pswString
{
    if (self = [super init]) {
        
        _nameString = nameString;
        _pswString = pswString;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     self.edgesForExtendedLayout = UIRectEdgeNone;//跳过64
    _phoneTF.delegate =self;
    _emailTF.delegate =self;
    _nameTF.delegate =self;
    [_affirmBtn.layer setMasksToBounds:YES];
    [_affirmBtn.layer setCornerRadius:3]; //设置矩圆角半径
    
    [_bgView.layer setMasksToBounds:YES];
    [_bgView.layer setCornerRadius:5]; //设置矩圆角半径
    
    [_bgView.layer setBorderWidth:1];  //边框宽度

    [_bgView.layer setBorderColor:ZTX_DI_CORLOR.CGColor];//边框颜色
    
    _back.tintColor = APPCOLOR;
    _NavBar.titleTextAttributes = @{NSForegroundColorAttributeName: APPCOLOR, NSFontAttributeName : [UIFont systemFontOfSize:20]};
}
- (IBAction)goBackBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//男
- (IBAction)manBtnClick:(id)sender {
    _manLab.textColor = APPCOLOR;
    _womanLab.textColor = [UIColor blackColor];
    _manImage.image = [UIImage imageNamed:@"GLDZicon_08"];
    _womanImage.image = [UIImage imageNamed:@"GLDZicon_13"];
}
//女
- (IBAction)womanBtnClick:(id)sender {
    _manLab.textColor = [UIColor blackColor];
    _womanLab.textColor = APPCOLOR;
    _manImage.image = [UIImage imageNamed:@"GLDZicon_13"];
    _womanImage.image = [UIImage imageNamed:@"GLDZicon_08"];
}

//日期选择
- (IBAction)dateBtnClick:(id)sender {
    
    JHSelector *selector = [[JHSelector alloc]init];
    [selector.btn setTitle:@"取消" forState:UIControlStateNormal];
    [selector.btn2 setTitle:@"确定" forState:UIControlStateNormal];
    
    __weak __typeof__(JHSelector *) weakSelector = selector;
    selector.clickedBtnIndexBlock = ^(NSInteger index)
    {
        if (index == 0)
        {
        }
        else{
            _dateBtn.hidden = YES;
            _dateImage.hidden = YES;
            _dateLab.text = weakSelector.dateString;
        }
    };
    [selector show];

}
//确认
- (IBAction)affirmBtnClick:(id)sender {
    
      [self network];
}
- (void)network{
    
    NSDictionary *params = @{
                             @"UserName"     : _nameString,
                             @"UserPassword" : _pswString,
                             @"Nick"         : @"测试111",
                             @"IsTrans"      : @"false",
                             @"RowStatus"    : @"CREATE"
                             };
    
    NSString *paramsString = [NetWorkManager dictionaryToJson:params];
    
    NSDictionary *paraments = @{
                                @"Command" : @"onAdd",
                                @"TableId" : @"294",
                                @"Params"  : paramsString,
                                @"Id"      : @""
                                };
    
    
    [NetWorkManager requestWithMethodName:@"12082/api/Rest/RestProcess?" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"注册 : %@", object);
        NSLog(@"%@", object[@"Message"]);
        NSLog(@"%@", object[@"TableDisplayName"]);
        
        
        if ([object[@"Code"] isEqualToString:@"0"]) {
            [EBUtility prompt:@"注册成功!" with:self];
            [self.view.window resignFirstResponder];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else if ([object[@"Code"] isEqualToString:@"-222"]){
            [self showAlert:@"该用户名已被注册"];
            [EBUtility prompt:@"该用户名已被注册" with:self];
        }
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"注册网络请求失败 : %@", error);
    } progress:nil];
}

//弹出警示框
- (void)showAlert:(NSString *)string{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@", string] message:nil preferredStyle:UIAlertControllerStyleAlert];
    //注册成功
    if ([string isEqualToString:@"注册成功!"]) {
        UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *done) {
//            LoginViewController *login = [[LoginViewController alloc] init];
//            login.UserName.text = _userName.text;
//            [self.navigationController pushViewController:login animated:YES];
        }];
        [alert addAction:done];
        
        //注册失败
    }else{
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancle];
    }
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark --去键盘

- (void)_handleEndEditing
{
    [self.view endEditing:YES];
    
}

//当用户按下return去键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
 
    return YES;
    
}

//空白去键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view.window endEditing:YES];
    
}
@end
